package com.coocaa.ops.api.v1.content.service.business;

import com.coocaa.ops.api.base.model.BaseQueryModel;
import com.coocaa.ops.api.common.model.HeaderModel;
import com.coocaa.ops.api.v1.content.cache.IPanelInfoCache;
import com.coocaa.ops.api.v1.content.model.internal.PanelSimpleInfo;
import com.coocaa.ops.api.v1.content.model.internal.PanelSimpleInfoWithDmp;
import com.coocaa.ops.api.v1.content.model.internal.TabContentInfo;
import com.coocaa.ops.api.v1.content.model.output.normal.Container;
import com.coocaa.ops.api.v1.content.model.query.PanelQueryModel;
import com.coocaa.ops.api.v1.content.model.query.TabQueryModel;
import com.coocaa.ops.api.v1.content.provider.IPanelProvider;
import com.coocaa.ops.api.v1.content.service.internal.DmpTabPanelService;
import com.coocaa.ops.api.v1.content.util.TabContentQueryUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * @author tangminghong
 * @date 2020-06-28
 */
@Service
public class NormalTabServiceImpl implements INormalTabService {

    @Autowired
    private IPanelInfoCache panelInfoCache;

    @Autowired
    private IPanelProvider panelProvider;

    @Autowired
    private DmpTabPanelService dmpTabPanelService;

    @Override
    public TabContentInfo getNormalTabContent(TabQueryModel tabQueryModel) {
        /**
         * 逻辑：
         * 获取版块基本信息列表
         * 筛选列表
         * 根据列表获取版块的内容
         */
        PanelQueryModel panelQueryModel = TabContentQueryUtil.buildPanelQueryModel(tabQueryModel);
        List<PanelSimpleInfo> panelList = panelInfoCache.getAllPanels(panelQueryModel.getTabId(), panelQueryModel.getPolicyCategory());
        if (Objects.equals(tabQueryModel.getHasDmp(),1)){
            panelList = handlePanelDmp(panelList, panelQueryModel);
        }
        TabContentInfo result = new TabContentInfo();
        if (panelList == null) {
            result.setTotalCount(0);
        } else {
            result.setTotalCount(panelList.size());
            result.setContent(getPanelContents(panelList, panelQueryModel));
        }
        return result;
    }

    private List<Container> getPanelContents(List<PanelSimpleInfo> panelSimpleList, PanelQueryModel queryModel) {
        List<PanelSimpleInfo> panelPageList = pagePanel(panelSimpleList, queryModel);
        return panelProvider.getPanelContents(panelPageList, queryModel);

    }

    private List<PanelSimpleInfo> handlePanelDmp (List<PanelSimpleInfo> normalPanel, PanelQueryModel queryModel) {

        return dmpTabPanelService.handlePanelDmp(normalPanel, queryModel);
    }

    private List<PanelSimpleInfo> pagePanel(List<PanelSimpleInfo> origPanelList, PanelQueryModel queryModel) {
        if (queryModel.getOffset() >= origPanelList.size()) {
            return Collections.emptyList();
        }

        int start = queryModel.getOffset();
        int end = queryModel.getLimit() + start;
        end = end > origPanelList.size() ? origPanelList.size() : end;

        return origPanelList.subList(start, end);
    }
}
